<!--
 * @Author: 孙大大
 * @Date: 2022-03-25 16:47:39
 * @LastEditTime: 2022-03-28 15:58:01
 * @FilePath: \vue-admin-template-master\src\views\departments\index.vue
-->
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- 缺少treeNode -->
        <tree-tools
          :tree-node="company"
          :is-root="false"
          @addDepts="addDepts"
        />
        <hr />
        <!--放置一个属性   这里的props和我们之前学习的父传子 的props没关系-->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 说明el-tree里面的这个内容 就是插槽内容 => 填坑内容  => 有多少个节点循环多少次 -->
          <!-- scope-scope 是 tree组件传给每个节点的插槽的内容的数据 -->
          <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
          />
        </el-tree>
      </el-card>
      <!-- 添加部门弹出层 -->
      <addDept :showDialog="showDialog" :node="node"></addDept>
    </div>
  </div>
</template>

<script>
import addDept from "@/views/departments/components/add-dept";
import treeTools from "@/views/departments/components/tree-tools";
import { getDepartments } from "@/api/departments";
import { tranListToTreeData } from "@/utils";
export default {
  components: { treeTools, addDept },
  name: "Departments",
  data() {
    return {
      defaultProps: {
        label: "name",
      },
      departs: [
        {
          name: "总裁办",
          manager: "曹操",
          children: [{ name: "董事会", manager: "曹丕" }],
        },
        { name: "行政部", manager: "刘备" },
        { name: "人事部", manager: "孙权" },
      ],
      company: {
        name: "江苏传智播客教育科技股份有限公司",
        manager: "负责人",
        // id: "",
      },
      showDialog: false, // 默认不显示弹层
      node: {},
    };
  },
  created() {
    this.getDepartments();
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments();
      console.log(result);
      this.company = { name: result.companyName, manager: "负责人", id: "" };
      this.departs = result.depts; // 需要将其转化成树形结构
      this.departs = tranListToTreeData(result.depts, "");
    },
    addDepts(node) {
      this.showDialog = true; // 显示弹层
      this.node = node;
    },
  },
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
